package top.jle.jpa.basic.dao;


import top.jle.jpa.basic.dao.PropertyFilter.MatchType;

import java.util.Arrays;
import java.util.Objects;

/**
 * 提供PreFilter简便的生成方法,同时隐藏PreFilter实现类细节
 * 
 * @author jim
 */
public class PreFilters {
	private PreFilters(){}
	private static PreFilter[] trimFilters(PreFilter[] preFilters){
		if(preFilters != null){
			return Arrays.stream(preFilters).dropWhile(Objects::isNull).toArray(PreFilter[]::new);
		}
		return new PreFilter[]{};
	}
	/**
	 * 将过滤器之间用or连接,构成新的过滤器
	 * 
	 * @param preFilters 长度需大于等于2
	 */
	public static PreFilter or(PreFilter... preFilters) {
		PreFilter[] trimmedFilters = PreFilters.trimFilters(preFilters);
		if(trimmedFilters.length==0){
			return null;
		}else if(trimmedFilters.length == 1){
			return trimmedFilters[0];
		}else{
			return new OrPreFilter(trimmedFilters);
		}
		
	}
	
	public static PreFilter and(PreFilter ...preFilters){
		PreFilter[] trimmedFilters = PreFilters.trimFilters(preFilters);
		if(trimmedFilters.length == 0){
			return null;
		}else if(trimmedFilters.length == 1){
			return trimmedFilters[0];
		}else{
			return new AndPreFilter(trimmedFilters);
		}		
	}
	
	public static PreFilter not(PreFilter preFilter) {
		if(preFilter != null){
			return new NotPreFilter(preFilter);
		}else{
			return null;
		}
		
	}

	/**
	 * 等于(忽略大小写?)
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter eq(String propertyName, Object value) {
		return new PropertyFilter(propertyName, MatchType.EQ,value);
	}
	
	/**
	 * 小于
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter lt(String propertyName, Object value) {
		return new PropertyFilter(propertyName, MatchType.LESS_THAN,value);
	}
	/**
	 * 大于
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter gt(String propertyName, Object value) {
		return new PropertyFilter(propertyName, MatchType.GREATER_THAN,value);
	}
	
	/**
	 * 小于或等于
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter le(String propertyName, Object value) {
		return new PropertyFilter(propertyName, MatchType.LESS_THAN_OR_EQ,value);
	}
	
	/**
	 * 大于或等于
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter ge(String propertyName, Object value) {
		return new PropertyFilter(propertyName, MatchType.GREATER_THAN_OR_EQ,value);
	}

	/**
	 * in
	 * 
	 * @param propertyName
	 * @param values
	 * @return
	 */
	public static PreFilter in(String propertyName, Object... values) {
		return new PropertyFilter(propertyName, MatchType.IN,values);
	}
	
	/**
	 * not in
	 * 
	 * @param propertyName
	 * @param values
	 * @return
	 */
	public static PreFilter notIn(String propertyName, Object... values) {
		return new PropertyFilter(propertyName, MatchType.NOT_IN,values);
	}

	/**
	 * 不等于(忽略大小写)
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter notEq(String propertyName, Object value) {
		return new PropertyFilter(propertyName, MatchType.NOT_EQ,value);
	}
		
	/**
	 * like '%value%'(忽略大小写)
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter like(String propertyName, String value) {
		return new PropertyFilter(propertyName, MatchType.LIKE,value);
	}
	
	/**
	 * not like
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter notLike(String propertyName, String value) {
		return new PropertyFilter(propertyName, MatchType.NOT_LIKE,value);
	}

	/**
	 * start with value
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter startWith(String propertyName, String value) {
		return new PropertyFilter(propertyName, MatchType.START_WITH,value);
	}

	/**
	 * end with value
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public static PreFilter endWith(String propertyName, String value) {
		return new PropertyFilter(propertyName, MatchType.END_WITH,value);
	}

	/**
	 * is null
	 * 
	 * @param  propertyName
	 * @return
	 */
	public static PreFilter isNull(String propertyName) {
		return new PropertyFilter(propertyName, MatchType.IS_NULL);
	}
	
	/**
	 * is not null
	 * 
	 * @param propertyName
	 * @return
	 */
	public static PreFilter isNotNull(String propertyName) {
		return new PropertyFilter(propertyName, MatchType.IS_NOT_NULL);
	}
	
	public static PreFilter between(String propertyName ,Object start,Object end){
		return new PropertyFilter(propertyName,MatchType.BETWEEN,start,end);
	}

	public static PreFilter memberOf(String propertyName,Object value) {
		return new PropertyFilter(propertyName,MatchType.ARRAY_CONTAINS,value);
	}

	public static PreFilter jsonLike(String propertyName,String subPropertyName,String value){
		return new JSONPropertyFilter(propertyName,subPropertyName,MatchType.LIKE,value);
	}
	public static PreFilter jsonEq(String propertyName,String subPropertyName,Object value){
		return new JSONPropertyFilter(propertyName,subPropertyName,MatchType.EQ,value);
	}
	public static PreFilter jsonGe(String propertyName,String subPropertyName,Long value){
		return new JSONPropertyFilter(propertyName,subPropertyName,MatchType.GREATER_THAN_OR_EQ,value);
	}
	public static PreFilter jsonGt(String propertyName,String subPropertyName,Long value){
		return new JSONPropertyFilter(propertyName,subPropertyName,MatchType.GREATER_THAN,value);
	}
	public static PreFilter jsonLe(String propertyName,String subPropertyName,Long value){
		return new JSONPropertyFilter(propertyName,subPropertyName,MatchType.LESS_THAN_OR_EQ,value);
	}
	public static PreFilter jsonLt(String propertyName,String subPropertyName,Long value){
		return new JSONPropertyFilter(propertyName,subPropertyName,MatchType.LESS_THAN,value);
	}
}
	


